package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dto.ChannelDto;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by ZYJ on 2021/5/22 20:58
 */
@Service
public class AdChannelServiceImpl  extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 根据名称分页查询频道列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.参数检测
        dto.checkParam();
        //2.按名称模糊分页查询
        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNoneBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        IPage result = page(page,lambdaQueryWrapper);
        //3.结果封装
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }
    /**
     * 频道新增
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {

        if(adChannel.getIsDefault()!=null && adChannel.getIsDefault()){
            //将数据库中 是默认状态的频道查询出来
            List<AdChannel> list = list(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getIsDefault, true));
            list.forEach(adChannel1 -> {
                adChannel.setIsDefault(false);
                updateById(adChannel);
            });

        }
        //2. 执行新增
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //3. 返回结果
        return ResponseResult.okResult();
    }
    /**
     * 频道修改
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        if(adChannel.getId()==null||null ==adChannel){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //先查
        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.修改
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel channel = getById(id);
        if(channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 启用状态下不能删除
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"启用状态不能删除");
        }
        //删除频道
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
